package com.qf.shiro;



import com.qf.entity.Emplyee;
import com.qf.service.EmplyeeService;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component("myRealm")
public class MyRealm extends AuthorizingRealm {

    @Resource
    private EmplyeeService emplyeeService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        String ename = getAvailablePrincipal(principals).toString();

        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        AuthenticationInfo info=null;
        UsernamePasswordToken tk= (UsernamePasswordToken) token;
        String ename = tk.getUsername();
        char[] ps = tk.getPassword();
        String epassword=new String(ps);
        System.out.println("shiro:"+ename+epassword);
        Emplyee emplyee = emplyeeService.login(ename, epassword);
        System.out.println(emplyee);
        if(emplyee!=null && emplyee.getEid()!=0){
            info=new SimpleAuthenticationInfo(ename,epassword,getName());
        }
        return info;
    }
}
